<!-- 角色权限绑定记录 -->
<template>
  <div class="module_">
    <div class="flex-space-between mb8 search_box">
      <p></p>
      <el-button
        class="mr8"
        size="medium"
        type="primary"
        @click="handle(null, {}, 3)"
        >添加角色权限绑定</el-button>
    </div>
    <el-table
      :data="tableData"
      size="medium"
      class="flex-1"
      height="150"
      border
      highlight-current-row
      v-loading="tableLoading"
      stripe
      @sort-change="tableSortChange"
    > 
    <el-table-column
        align="center"
        prop="employee"
        label="职员信息"
        min-width="550"
        sortable="custom"
        >
          <el-table-column
            align="center"
            prop="employee.name"
            label="职员名称"
            width="150"
            sortable="custom"
            >
          </el-table-column>
          <el-table-column
            align="center"
            prop="employee.code"
            label="职员工号"
            width="150"
            sortable="custom"
            >
          </el-table-column>
          <!-- <el-table-column
            align="center"
            prop="employee.position"
            label="职员岗位"
            width="150"
            sortable="custom"
            >
          </el-table-column>
          <el-table-column
            align="center"
            prop="employee.coefficient"
            label="系数"
            width="120"
            sortable="custom"
            >
          </el-table-column> -->
      </el-table-column>
      <el-table-column
        align="center"
        prop="role"
        label="角色信息"
        min-width="550"
        sortable="custom"
        >
          <el-table-column
            align="center"
            prop="role.name"
            label="角色名称"
            width="150"
            sortable="custom"
            >
          </el-table-column>
          <el-table-column
            align="center"
            prop="role.display_name"
            label="角色显示名称"
            width="150"
            sortable="custom"
            >
          </el-table-column>
          <el-table-column
            align="center"
            prop="role.description"
            label="角色描述"
            min-width="200"
            sortable="custom"
            >
          </el-table-column>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="medium" @click="handle(scope.$index, scope.row, 2)"
            >修改</el-button>
          <el-button
            size="medium"
            type="danger"
            @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>

    <el-dialog
      class="dialog"
      :visible.sync="editFormVisible"
      width="50%"
      @click="editFormVisible = false"
      :close-on-click-modal='false'
      :close-on-press-escape='false'>
      <el-form autocomplete="off"  label-width="120px" :model="editForm">
        <el-row>
          <el-col :span="10">
            <el-form-item label="职员" class="tip">
              <el-select v-model="employees_id" placeholder="请选择">
                <el-option
                  v-for="item in employeeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="角色" class="tip">
              <el-select v-model="roles_id" placeholder="请选择">
                <el-option
                  v-for="item in roleList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit" :loading='btnLoading'>确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  Apiemployeeroles,
  ApiemployeerolesAdd,
  ApiemployeerolesPut,
  ApiemployeerolesDel,
  Apiemployees,
  Apiroles
} from "@/api";
import { debounce } from "@/utils";
import {mapState,mapActions} from 'vuex'
export default {
  data() {
    return {
      tableData: [],
      editFormVisible: false,
      editForm: {}, //修改
      pageparm: {
        page: 1,
        perPage: 20,
        total: null,
        sort_str: "updated_at",
        sort_order: "desc",
      },
      formInline: {},
      tableLoading: false,
      roles_id: '',
      employees_id :'',
      employees_name:'',
      change_id: '',  //修改的id

      btnLoading: false,
      middleList: [],


      roleList: [],  //角色列表
      employeeList: [] //职员列表
    };
  },
  mounted() {
    this.getList();
    setTimeout(()=>{
      Apiroles({allPage:2}).then((res) => {
        const data = res.data || []
        this.roleList = data
      })
      Apiemployees({allPage:2}).then((res) => {
        const data = res.data || []
        this.employeeList = data
      })
    },30)
  },
  methods: {
    onChange(value){
      this.employees_id = value.id
    },
    tableSortChange(column) {
      if (column.order === "descending") {
        this.pageparm.sort_str = column.prop;
        this.pageparm.sort_order = "desc";
      } else if (column.order === "ascending") {
        this.pageparm.sort_str = column.prop;
        this.pageparm.sort_order = "asc";
      }else{
        this.pageparm.sort_str = null;
        this.pageparm.sort_order = null;
      }
      this.getList();
    },
    //搜索
    search() {},
    //分页
    callFather(pageparm) {
      this.pageparm.page = pageparm.page;
      this.pageparm.perPage = pageparm.perPage;
      this.getList();
    },
    getList() {
      this.tableLoading = true;
      Apiemployeeroles(this.pageparm).then(({ data, page }) => {
          this.tableData = data || [];
          this.pageparm.total = page.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    handle(index, value, i) {
      if(i === 3){
        this.change_id = null
        this.roles_id = null,
        this.employees_id = null
        this.middleList = []
      }else if(i == 2){
        this.change_id = value.id
        this.roles_id = value.role.id
        this.employees_id = value.employee.id
        this.employees_name = value.employee.name
        this.middleList = [{id:value.employee.id,name:value.employee.name}]
      } 
      this.editFormVisible = true;
    },
    deleteUser(index, value) {
      this.$confirm("此操作将删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: '加载中...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          });
          ApiemployeerolesDel(value.id).then((res) => {
            this.getList();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          }).finally(()=>{
            loading.close()
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    submit: debounce(function () {
      //提交  修改或新增

      if (!this.roles_id || this.employees_id.length === 0) {
        this.$message.error("请输入全部信息");
        return;
      }
      this.btnLoading = true
      let options = {
        roles_id: this.roles_id,
        employees_id: this.employees_id,
      };
      if (this.change_id) {
        //有ID就是修改
        ApiemployeerolesPut(options, this.change_id)
          .then((res) => {
            this.$message({
              type: "success",
              message: "修改成功!",
            });
            this.editFormVisible = false;
            this.getList();
          })
          .catch(() => {
            this.$message({
              type: "error",
              message: "修改失败!",
            });
          }).finally(()=>{
            this.btnLoading = false
          });
      } else {
        ApiemployeerolesAdd(options)
          .then((res) => {
            this.$message({
              type: "success",
              message: "添加成功!",
            });
            this.editFormVisible = false;
            this.getList();
          })
          .catch(() => {
            this.$message({
              type: "error",
              message: "添加失败!",
            });
          }).finally(()=>{
            this.btnLoading = false
          });
      }
    },500,true),
  },
};
</script>

<style lang='less' scoped>
.dialog {
  .el-col-12 {
    width: 70%;
  }
}
</style>